package com.oniontech.stamptour.fragment.app;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.oniontech.stamptour.MainActivity;
import com.oniontech.stamptour.R;
import com.oniontech.stamptour.fragment.DefaultFragment;

/**
 * @author limsy
 *
 */
public class StampMainFragment extends DefaultFragment {


    View view_1, view_2, view_3, view_4, view_5, view_6, view_7, view_8, view_9;
    View []btn_view = new View[9];

    int []m_drawable_n = {R.drawable.main_menu01_n_selector, R.drawable.main_menu02_n_selector,R.drawable.main_menu03_n_selector,R.drawable.main_menu04_n_selector,R.drawable.main_menu05_n_selector,
            R.drawable.main_menu06_n_selector,R.drawable.main_menu07_n_selector,R.drawable.main_menu08_n_selector,R.drawable.main_menu09_n_selector};

    int []m_drawable_s = {R.drawable.main_menu01_s_selector, R.drawable.main_menu02_s_selector,R.drawable.main_menu03_s_selector,R.drawable.main_menu04_s_selector,R.drawable.main_menu05_s_selector,
            R.drawable.main_menu06_s_selector,R.drawable.main_menu07_s_selector,R.drawable.main_menu08_s_selector,R.drawable.main_menu09_s_selector};

    public  int m_totalcnt = 0;

    public Button btn_mail;


    @Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.stampmain, null);
		view.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return true;
			}
		});

        view_1 = view.findViewById(R.id.view_1);
        view_1.setOnClickListener(iconclick);
        btn_view[0] = view_1;

        view_2 = view.findViewById(R.id.view_2);
        view_2.setOnClickListener(iconclick);
        btn_view[1] = view_2;

        view_3 = view.findViewById(R.id.view_3);
        view_3.setOnClickListener(iconclick);
        btn_view[2] = view_3;

        view_4 = view.findViewById(R.id.view_4);
        view_4.setOnClickListener(iconclick);
        btn_view[3] = view_4;

        view_5 = view.findViewById(R.id.view_5);
        view_5.setOnClickListener(iconclick);
        btn_view[4] = view_5;

        view_6 = view.findViewById(R.id.view_6);
        view_6.setOnClickListener(iconclick);
        btn_view[5] = view_6;

        view_7 = view.findViewById(R.id.view_7);
        view_7.setOnClickListener(iconclick);
        btn_view[6] = view_7;

        view_8 = view.findViewById(R.id.view_8);
        view_8.setOnClickListener(iconclick);
        btn_view[7] = view_8;

        view_9 = view.findViewById(R.id.view_9);
        view_9.setOnClickListener(iconclick);
        btn_view[8] = view_9;

        btn_mail = (Button)view.findViewById(R.id.btn_mail);
        btn_mail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(m_totalcnt > 2) {
                    //메일 쓰기
                    Fragment fragment = new StampMainToMailFragment();
                    ((MainActivity) getActivity()).setFragment(fragment, MainActivity.FRAGMENT_ADD);
                }
            }
        });

        setMainIcon();

		return view;
	}

    View.OnClickListener iconclick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Fragment fragment = new StampMainToListFragment();
            ((StampMainToListFragment)fragment).onFragmentData(v.getTag());
			((MainActivity) getActivity()).setFragment(fragment, MainActivity.FRAGMENT_ADD);
        }
    };

    public void setMainIcon() {
        m_DBMgr.openPointDB(DB_TYPE_ADDRESS, true);

        m_totalcnt = 0;

        String str = null;

        StringBuffer sb = new StringBuffer();

        sb.append("select code , name , \n");
        sb.append("case when tcnt = ifnull(vcnt,0) then '1' else '0' end as visit ,\n");
        sb.append("case when tcnt = ifnull(vcnt,0) then mainImgS else mainImgN end as mainImgD ,\n");
        sb.append("mainImgS\n");
        sb.append("from theme t \n");
        sb.append("inner join \n");
        sb.append("( select theme_code , count(*) as tcnt from contents group by theme_code) a \n");
        sb.append("on t.code = a.theme_code \n");
        sb.append("left join \n");
        sb.append("( select theme_code , count(*) as vcnt from contents where visit = 1 group by theme_code) b \n");
        sb.append("on t.code = b.theme_code\n");
        sb.append("order by oder\n");

        Cursor cursor = m_DBMgr.DBQuery(sb.toString());

        if (cursor == null || !cursor.moveToFirst())
        {
            m_DBMgr.closeDB();
            return;
        }

        int cnt = cursor.getCount();

        if(cnt == 0) {
            return;
        }

        for (int i = 0; i < cnt; i++) {
            if(cursor.getInt(cursor.getColumnIndex("visit")) == 0) {
                ((ImageView)btn_view[i]).setImageResource(m_drawable_n[i]);
            }else {
                ((ImageView)btn_view[i]).setImageResource(m_drawable_s[i]);
                m_totalcnt++;

            }
            cursor.moveToNext();
        }

        cursor.close();
        m_DBMgr.closeDB();

        if(m_totalcnt > 2) {
            btn_mail.setBackgroundResource(R.drawable.btn_subscribe_n_selector);
        }else {
            btn_mail.setBackgroundResource(R.drawable.btn_subscribe_d_selector);
        }
    }

}
